package pl.uz.zgora.plan;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class WidgetUpdateService extends Service{

	private Timer updatingTimer = null;
	/** Częstotliwośc odświeżania widgetu, w minutach */
	private static int UPDATE_FREQ = 20;

	private TimerTask updateTask = new TimerTask() {
        @Override
        public void run() {
        	updateWidget();
        }
	};
	
	public static void setUpdateFreq(final int updateFreq) {
		UPDATE_FREQ = updateFreq;
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void updateWidget() {
		Log.d("widget_service", "updateWidget()");
		
		final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance( this );
		final ComponentName thisWidget = new ComponentName(this, NextClassWidget.class);
		int[] ids = appWidgetManager.getAppWidgetIds(thisWidget);
		if(ids != null) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   			
			for(int i = 0; i < ids.length; i++) {
					NextClassWidget.refreshWidgetUI(this, ids[i]);
			}
		}

	}
	
	@Override
	public void onDestroy() {
		updatingTimer.cancel();
		Log.d("widget_service", "onDestroy()");
		super.onDestroy();
	}
	
	@Override
    public void onCreate() {
        super.onCreate();
        Log.d("widget_service", "onCreate()");
        updatingTimer = new Timer();
	}
	
	@Override
    public void onStart(Intent intent, int startId) {
		 Log.d("widget_service", "onStart()");
		 updatingTimer.scheduleAtFixedRate(updateTask, 1*60*1000, UPDATE_FREQ*60*1000);
		 super.onStart(intent, startId);
	}

}
